<template>
  <div>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="用户名">
        <el-input v-model="form.user"></el-input>
      </el-form-item>
       <el-form-item label="用户密码">
        <el-input v-model="form.pwd"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">登录</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import axios from 'axios'
export default {
  data() {
    return {
      form: {
        user: '',
        pwd: ''
      }
    };
  },
  methods: {
    onSubmit() {
      // 发请求
      axios.post('/api/user/login', this.form).then(resp => {
        // 解构  数据
        const { code, mess, token } = resp.data
        if(code === 200) {
          // 提示成功
          this.$message({
            message: '登录成功， 2s之后跳转首页...',
            type: 'success'
          });
          // 保存token
          localStorage.setItem('token', token)
          // 2s 之后跳转首页
          setTimeout(() => {
            this.$router.push('/')
          }, 2000)
        } else {
          // 错误提示
           this.$message.error(mess);
        }
      })
    }
  }
};
</script>